define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'auction/index' + location.search,
                    add_url: 'auction/add',
                    edit_url: 'auction/edit',
                    del_url: 'auction/del',
                    multi_url: 'auction/multi',
                    import_url: 'auction/import',
                    table: 'auction',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'auction_id',
                sortName: 'auction_id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'auction_id', title: __('Auction_id')},
                        {field: 'name', title: __('Name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'images', title: __('Images'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
                        {field: 'start_time', title: __('Start_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'end_time', title: __('End_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'deposit', title: __('Deposit'), operate:'BETWEEN'},
                        {field: 'status', title: __('Status'), formatter: Table.api.formatter.status, searchList: {0: __('待开始'), 1: __('进行中'), 2: __('已结束'), 3: __('已暂缓'), 4: __('已终止'), 5: __('已撤回'), 6: __('已暂停')}},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons:[
                                {
                                    name: 'edit',
                                    icon: 'fa fa-pencil',
                                    title: __('Edit'),
                                    extend: 'data-toggle="tooltip"',
                                    classname: 'btn btn-xs btn-success btn-editone',
                                    visible: function (row) {
                                        if(row.status === 0){
                                            return true;
                                        }
                                    }
                                },
                                {
                                    name: 'detail',
                                    text: __('查看出价记录'),
                                    icon: '',
                                    refresh:true,
                                    classname: 'btn btn-info btn-xs',
                                    url: 'auctionbidlog/index',
                                    extend:' target="_blank"',
                                },
                                {
                                    name: 'cancel',
                                    text: __('撤回'),
                                    confirm:'确认撤回吗?',
                                    icon: '',
                                    refresh:true,
                                    classname: 'btn btn-info btn-xs btn-detail  btn-ajax',
                                    url: 'auction/cancel',
                                    visible:function(row){
                                        if(row.is_cancel == 0&&row.status != 2){
                                            return true;
                                        }
                                    }
                                },
                                {
                                    name: 'stop',
                                    text: __('中止'),
                                    confirm:'确认中止吗?',
                                    icon: '',
                                    refresh:true,
                                    classname: 'btn btn-info btn-xs btn-danger  btn-ajax',
                                    url: 'auction/stop',
                                    visible:function(row){
                                        if(row.is_stop == 0&&row.status != 2){
                                            return true;
                                        }
                                    }
                                },
                                {
                                    name: 'pause',
                                    text: __('暂停'),
                                    confirm:'确认暂停吗?',
                                    icon: '',
                                    refresh:true,
                                    classname: 'btn btn-info btn-xs btn-detail  btn-ajax',
                                    url: 'auction/pause',
                                    visible:function(row){
                                        if(row.is_pause == 0&&row.status != 2){
                                            return true;
                                        }
                                    }
                                },
                                {
                                    name: 'defer',
                                    text: __('暂缓'),
                                    confirm:'确认暂缓吗?',
                                    icon: '',
                                    refresh:true,
                                    classname: 'btn btn-info btn-xs btn-detail  btn-ajax',
                                    url: 'auction/defer',
                                    visible:function(row){
                                        if(row.is_defer == 0&&row.status != 2){
                                            return true;
                                        }
                                    }
                                },
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'auction/recyclebin' + location.search,
                pk: 'auction_id',
                sortName: 'auction_id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'auction_id', title: __('Id')},
                        {field: 'name', title: __('Name'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'auction/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'auction/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
